<?php
/**
 * Created by PhpStorm.
 * User: molong
 * Date: 2019/1/6
 * Time: 1:04
 */

namespace app\admin\logic;


use think\Console;

class LifeInfo extends AdminBase
{

    /**
     * @function 获取一个
     * @param array $param
     * @param array $where
     * @param bool $field
     * @return array
     */
    public function getOne($param = [], $where = [], $field = true){
        if (empty($param)) {
            return [RESULT_ERROR, '非法操作'];
        }
        $ids = (int)$param['ids'];
        $this->modelLifeInfo->alias('m');
        // 条件
        $where['m.id'] = ['=', $ids];
        // 查数据库获取数据
        $lifeInfo = $this->modelLifeInfo->getInfo($where, $field);
        return $lifeInfo;

    }


    public function updateLifeInfo($param = []) {
        $ids = (int)$param['id'];
        // 指定数据库表别名
        $this->modelLifeAccount->alias('m');
        // 条件
        $where['m.id'] = ['=', $ids];
        // 查数据库获取数据
        $lifeAccount = $this->modelLifeAccount->getInfo($where);

        vendor('alipay.aop.AopClient');
        vendor('alipay.aop.request.AlipayOpenPublicInfoModifyRequest');

        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $lifeAccount['appid'];
        $aop->rsaPrivateKey = $lifeAccount['merchant_private_key'];
        $aop->alipayrsaPublicKey = $lifeAccount['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new \AlipayOpenPublicInfoModifyRequest();
        $request->setBizContent("{" .
            "\"app_name\":". $param['app_name'] ."," .
            "\"logo_url\":". $param['logo_url'] ."," .
            "\"public_greeting\":". $param['public_greeting'] ."," .
            "\"introduction\":". $param['introduction'] ."," .
            "\"background_url\":". $param['background_url'] ." }");
        $result = $aop->execute ( $request );
//        dump($result); exit;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            $url = url('lifeAccountList');

            $result_1 = $this->modelLifeInfo->setInfo($param);

            return $result_1 ? [RESULT_SUCCESS, '生活号添加成功', $url] : [RESULT_ERROR, $this->modelLifeInfo->getError()];
        } else {
            return [RESULT_SUCCESS, '生活号添加失败'];
        }
    }

}
